package top.tenebrous.yzl.im.entity;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import top.tenebrous.yzl.im.contants.CommonConstants;
import top.tenebrous.yzl.im.enums.ProducerType;

import java.util.List;
import java.util.UUID;

/**
 * @author yazhang6
 */
@ConfigurationProperties(prefix = "yzl.im", ignoreInvalidFields = true)
@Data
@Component(value = "webSocketProperties")
public class ImWebSocketProperties {

    /**
     * 服务端实例名称
     */
    private String instanceServerName;

    /**
     * 客户端实例名称
     */
    private String instanceClientName;

    /**
     * 版本
     */
    private String version = CommonConstants.VERSION;

    /**
     * 后缀
     */
    private String serverSuffixPath = CommonConstants.BACK_SLASH;

    /**
     * 后缀
     */
    private String chatSuffixPath = CommonConstants.BACK_SLASH;

    /**
     * 是否打印banner
     */
    private boolean enablePrintBanner = true;

    /**
     * 是否开始敏感词过滤
     */
    private boolean enableOpenSensitiveWord = false;

    /**
     * 自定义敏感词文件名称
     */
    private String sensitiveWordName = CommonConstants.SENSITIVE_NAME;

    /**
     * 是否集群
     */
    private boolean enableColony = false;

    /**
     * 集群共享模式 默认redis
     */
    private ProducerType colonyShareType = ProducerType.REDIS;

    /**
     * 客户端地址
     */
    private List<ServerInfos> serverInfo;

    @Data
    public static class ServerInfos {

        private String path;

        private String clientName;
    }

    /**
     * 构建websocket请求路径
     *
     * @param contextPath       应用上下文路径
     * @return                  websocket连接路径
     */
    public String getPath(String contextPath, ImWebSocketProperties.ServerInfos extendAddress) {
        // 交易连接完整性
        if (StringUtils.isBlank(extendAddress.path)) {
            throw new RuntimeException("yzl-im:websocket server address not complete");
        }
        if (StringUtils.isBlank(this.getServerSuffixPath())) {
            this.serverSuffixPath  = CommonConstants.BACK_SLASH;
        }
        if (StringUtils.isBlank(this.getInstanceClientName())) {
            this.instanceClientName = UUID.randomUUID().toString().replace("-", "");;
        }

        String clientAddress = extendAddress.path;

        // 组装server链接
        // 组装客户端地址ws//localhost:8021
        clientAddress = extendAddress.path.endsWith(CommonConstants.BACK_SLASH) ? extendAddress.path : (extendAddress.path + CommonConstants.BACK_SLASH);

        // 组装上下文路径  /api/yzl-chat
        String context = contextPath;
        if (!CommonConstants.BACK_SLASH.equals(context)) {
            if (contextPath.startsWith(CommonConstants.BACK_SLASH)) {
                context = contextPath.substring(1);
            }
            if (!context.endsWith(CommonConstants.BACK_SLASH)) {
                context = context + CommonConstants.BACK_SLASH;
            }
        } else {
            context = contextPath.substring(1);
        }

        // 组装server路径   /v1/yzl/chat
        String serverSuffixPath = this.serverSuffixPath;
        if (this.serverSuffixPath.startsWith(CommonConstants.BACK_SLASH)) {
            serverSuffixPath = this.serverSuffixPath.substring(1);
        }
        if (serverSuffixPath.endsWith(CommonConstants.BACK_SLASH)) {
            serverSuffixPath = serverSuffixPath.substring(0, serverSuffixPath.length() - 1);
        }

        String clientName = StringUtils.isBlank(extendAddress.clientName) ? UUID.randomUUID().toString().replace("-", "") : extendAddress.clientName;

        return clientAddress + context + serverSuffixPath + "?type=INSTANCE&clientName=" +  clientName;
    }


}
